package com.ky.serialport.sensor;

import com.ky.serialport.bean.ComBean;
import com.ky.serialport.utils.MyFunc;

import static com.ky.serialport.utils.RandomUtils.myRandom;

/**
 * GYD50W矿用本安型无线电压变送器
 */
public class DianYaBianSongQiBean {

    public byte[] bRec;
    public Long time;
    public int power; // 电量
    public Float[] dyArray = new Float[5];
    public Float[] dlArray = new Float[5];
    public float dlVel= 0f; //均值
    public float dyVel= 0f; //均值
    public int signal; //信号
    public ComBean comBean;

    public DianYaBianSongQiBean() {

    }

    public DianYaBianSongQiBean(ComBean comBean ) {
        time = comBean.recTime;
        this.comBean = comBean;
        caculate(comBean.recData);
    }

    public DianYaBianSongQiBean(byte[] recData) {
        caculate(recData);
        time = System.currentTimeMillis();
    }

    public DianYaBianSongQiBean caculate(ComBean comBean){
        time = comBean.recTime;
        this.comBean = comBean;
        return caculate(comBean.recData);
    }


    public DianYaBianSongQiBean caculate(byte[] bRec) {
        power = MyFunc.getPower(bRec[34],bRec[35]);
        signal = MyFunc.getSignal(bRec[36]);
        this.bRec = bRec;


        dlArray[0] = MyFunc.twoByte2IntNoSign(bRec[14],bRec[15])/100f;
        dlArray[1] = MyFunc.twoByte2IntNoSign(bRec[16],bRec[17])/100f;
        dlArray[2] = MyFunc.twoByte2IntNoSign(bRec[18],bRec[19])/100f;
        dlArray[3] = MyFunc.twoByte2IntNoSign(bRec[20],bRec[21])/100f;
        dlArray[4] = MyFunc.twoByte2IntNoSign(bRec[22],bRec[23])/100f;

        dyArray[0] = MyFunc.twoByte2IntNoSign(bRec[24],bRec[25])/1000f;
        dyArray[1] = MyFunc.twoByte2IntNoSign(bRec[26],bRec[27])/1000f;
        dyArray[2] = MyFunc.twoByte2IntNoSign(bRec[28],bRec[29])/1000f;
        dyArray[3] = MyFunc.twoByte2IntNoSign(bRec[30],bRec[31])/1000f;
        dyArray[4] = MyFunc.twoByte2IntNoSign(bRec[32],bRec[33])/1000f;


        float dlALL = 0f,dyALL = 0f;

        for (int i = 0; i < dlArray.length; i++) {
            dlALL += dlArray[i];
            dyALL += dyArray[i];
        }

        dlVel = dlALL /5f;
        dyVel = dyALL /5f;

        return this;
    }

    public DianYaBianSongQiBean test() {
        time = System.currentTimeMillis();
        power = 300 + myRandom.nextInt(120);
        signal = 200;
        return this;
    }

}
